﻿using Api.Core.Common.Extension;
using Api.Core.IRepository.Role;
using Api.Core.IServices.User;
using Api.Core.Model;
using Api.Core.Model.Entity;
using Api.Core.Model.Table;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Api.Core.Model.ViewModel;

namespace Api.Core.Services.Group
{
    public class GroupRoleService : BaseService<GroupRoleTable>, IGroupRoleService
    {
        protected readonly IRoleService _roleService;
        protected readonly IGroupRoleRespository _groupRoleRespository;
         

        public GroupRoleService(IGroupRoleRespository groupRoleRespository, IRoleService roleService)
        {
            base.BaseDal = groupRoleRespository;
            _roleService = roleService;
            _groupRoleRespository = groupRoleRespository;
        }

        /// <summary>
        /// 根据GroupId获取对应的权限List
        /// </summary>
        /// <param name="groupid"></param>
        /// <returns></returns>
        public async Task<List<GroupRoleTable>> GetGroupRoles(string groupid)
        {
            var groupRoleList = await _groupRoleRespository.Query(x => x.GroupId == groupid);
            return groupRoleList;
        }


        /// <summary>
        /// 新增实体
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public async Task<int> AddGroupRole(GroupRoleTable table)
        {
            var result = await _groupRoleRespository.Add(table);
            return result;
        }

        /// <summary>
        /// 新增和更新组权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<BaseResponse<bool>> AddGroupRoles(GroupViewModel entity)
        {
            var result = await _groupRoleRespository.AddGroupRoles(entity);
            return Result(result);
        }

        /// <summary>
        /// 删除组权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> DelGroupRole(GroupViewModel entity)
        {
            var result = await _groupRoleRespository.DelGroupRole(entity);
            return result;
        }

        /// <summary>
        /// 根据GroupId获取组的权限信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<BaseResponse<GroupRoleEntity>> GetGroupRoles(GroupViewModel model)
        {
            var result = new BaseResponse<GroupRoleEntity>();
            var groupRole = await _groupRoleRespository.QueryOne(x => x.GroupId == model.GroupId);
            if (groupRole == null || groupRole.Id == 0)
            {
                result.msg = "用户组不存在";
                return result;
            }
            var roleList = await _roleService.Query(x => x.RoleId == groupRole.RoleId);
            result.data.GroupId = groupRole.GroupId;
            result.data.RoleList.AddRange(roleList);
            return result;
        }

        /// <summary>
        /// 根据条件获取分组
        /// </summary>
        /// <returns></returns>
        public async Task<BaseResponse<List<GroupTable>>> GetAllGroupsByCondition(GroupEntity moeld)
        {
            var res = await _groupRoleRespository.GetAllGroupsByCondition(moeld);
            return Result(res);
        }
    }
}
